package br.com.millenium.domain.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import lumis.portal.PortalException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;

public class FuncionarioManager implements IFuncionarioManager 
{

	@Override
	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFuncionarioManager#isDuplicateFuncionarioMatricula(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateFuncionarioMatricula( String funcionarioId, String funcionarioMatricula, ITransaction transaction ) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_Funcionario WHERE matricula = ? and id <> ?" );
			try
			{
				statement.setString( 1, funcionarioMatricula );
				statement.setString( 2, funcionarioId        );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFuncionarioManager#isDuplicateFuncionarioCPF(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateFuncionarioCPF( String funcionarioId, String funcionarioCPF, ITransaction transaction ) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_Funcionario WHERE cpf = ? and id <> ?" );
			try
			{
				statement.setString( 1, funcionarioCPF );
				statement.setString( 2, funcionarioId  );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFuncionarioManager#isDuplicateFuncionarioRG(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateFuncionarioRG( String funcionarioId, String funcionarioRG, ITransaction transaction ) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_Funcionario WHERE rg = ? and id <> ?" );
			try
			{
				statement.setString( 1, funcionarioRG );
				statement.setString( 2, funcionarioId );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.millenium.domain.manager.IFuncionarioManager#isDuplicateCargo(java.lang.String, java.lang.String, lumis.util.ITransaction)
	 */
	public boolean isDuplicateCargo( String cargoId, String cargoNome, ITransaction transaction ) throws PortalException 
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc)transaction;
			Connection       connection         = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement( "SELECT id FROM MM_Cargo WHERE nome = ? and id <> ?" );
			try
			{
				statement.setString( 1, cargoNome );
				statement.setString( 2, cargoId   );
				
				ResultSet rs = statement.executeQuery();
				if( rs.next() )
					return true;
				else
					return false;
			}
			finally
			{
				statement.close();
			}
		}
		catch (Exception e)
		{
			throw new PortalException(e.getMessage());
		}
	}

}
